package 力扣.树;

import sun.reflect.generics.tree.Tree;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
class TreeNode {
    public int val;
    public List<TreeNode> children;

    public TreeNode() {
    }
    public TreeNode(int _val) {
        val = _val;
    }
    public TreeNode(int _val, List<TreeNode> _children) {
        val = _val;
        children = _children;
    }
}
public class N叉树的层序遍历 {
    public static void main(String[] args) {

    }
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();
        Deque<TreeNode> deque = new LinkedList<>();
        if (root != null){
            deque.offerFirst(root);
        }
        while (deque.size() > 0){//队列中还有元素
            int size = deque.size();
            List<Integer> teList = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode te = deque.pollLast();
                assert te != null;
                teList.add(te.val);
                List<TreeNode> childrens = te.children;
                for (TreeNode children: childrens) {
                    deque.offerFirst(children);
                }
            }
            ans.add(teList);
        }
        return ans;
    }
}
